package com.ugoodtech.mdcc.core.service.impl;

import com.ugoodtech.mdcc.core.model.entity.Departments;
import com.ugoodtech.mdcc.core.model.entity.DepartmentsExample;
import com.ugoodtech.mdcc.core.model.entity.UserProfiles;
import com.ugoodtech.mdcc.core.model.entity.UserProfilesExample;
import com.ugoodtech.mdcc.core.model.entity.Users;
import com.ugoodtech.mdcc.core.model.entity.UsersExample;
import com.ugoodtech.mdcc.core.model.entity.UsersExample.Criteria;
import com.ugoodtech.mdcc.core.model.mapper.DepartmentsMapper;
import com.ugoodtech.mdcc.core.model.mapper.UserProfilesMapper;
import com.ugoodtech.mdcc.core.model.mapper.UsersMapper;
import com.ugoodtech.mdcc.core.service.MybatisUserService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Xiangcheng Li
 *
 * @author Xiangcheng Li
 */
@Service
@Slf4j
public class MybatisUserServiceImpl implements MybatisUserService {


    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserProfilesMapper userProfilesMapper;


    @Autowired
    private DepartmentsMapper departmentsMapper;


    /**
     * 筛选用户
     * @param userName
     * @param departments
     * @return
     */
    public List<UserProfiles> listUser(String userName, String[] departments){


        UsersExample usersExample = new UsersExample();
        Criteria usersExampleCriteria = usersExample.createCriteria();
        usersExampleCriteria.andEnabledEqualTo(true);
        usersExampleCriteria.andDeletedEqualTo(0);
        usersExampleCriteria.andUserFlagIn(Arrays.asList(0, 2));

        UserProfilesExample userProfilesExample = new UserProfilesExample();
        UserProfilesExample.Criteria userProfilesExampleCriteria = userProfilesExample.createCriteria();
        userProfilesExampleCriteria.andEnabledEqualTo(true);
        userProfilesExampleCriteria.andDeletedEqualTo(false);



        if (StringUtils.isNotEmpty(userName)) {
            userProfilesExampleCriteria.andRealnameNotLike(userName);
            }
        if (departments != null){
            usersExampleCriteria.andUserFlagIn(Arrays.asList(0, 2));
            usersExampleCriteria.andUsertypeIn(Arrays.asList(0,4,5));
            // 筛选部门
            List<Departments> departmentsList = listDepartment(Arrays.asList(departments));
            if (CollectionUtils.isEmpty(departmentsList)) {
                return new ArrayList<>(0);
            }
            List<Long> departmentIdList = departmentsList.stream().map(Departments::getId)
                .collect(Collectors.toList());
            userProfilesExampleCriteria.andDeparmentIdIn(departmentIdList);

        }
        List<Users> users = usersMapper.selectByExample(usersExample);
        if (CollectionUtils.isEmpty(users)) {
            return new ArrayList<>(0);
        }
        List<Long> userIdList = users.stream().map(Users::getId).collect(Collectors.toList());
        userProfilesExampleCriteria.andUserIdIn(userIdList);
        return userProfilesMapper.selectByExample(userProfilesExample);


    }

    public List<UserProfiles> listUserProfile(List<Long> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>(0);
        }
        UserProfilesExample userProfilesExample = new UserProfilesExample();
        UserProfilesExample.Criteria userProfilesExampleCriteria = userProfilesExample.createCriteria();
        userProfilesExampleCriteria.andUserIdIn(userIdList);
        return userProfilesMapper.selectByExample(userProfilesExample);
    }

    public List<Departments> listDepartment(List<String> nameList) {
        if (CollectionUtils.isEmpty(nameList)) {
            return new ArrayList<>(0);
        }
        DepartmentsExample departmentsExample = new DepartmentsExample();
        for (String name : nameList) {
            departmentsExample.or().andNameLike(name);
        }
        return departmentsMapper.selectByExample(departmentsExample);

    }

}
